package com.example.beikeyigou;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.beikeyigou.login.LoginFragment;
import com.example.beikeyigou.login.LoginVPAdapter;
import com.example.beikeyigou.login.RegisterFragment;

import java.util.ArrayList;
import java.util.List;

public class LoginRegActivity extends AppCompatActivity {

    TextView logintv,regtv;
    ViewPager loginvp;
    List<Fragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_reg);

        int flag = getIntent().getIntExtra("flag", 0);
        initView();
        setListener();
        initPager();
        setPagerPos(flag);
    }

    private void initPager() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new LoginFragment());
        fragmentList.add(new RegisterFragment());

        //将fragment放置在Viewport中显示在activity上
        LoginVPAdapter loginVPAdapter = new LoginVPAdapter(getSupportFragmentManager(),fragmentList);
        loginvp.setAdapter(loginVPAdapter);//设置适配器
    }

    private void setListener() {
        logintv.setOnClickListener(onClickListener);
        regtv.setOnClickListener(onClickListener);

        loginvp.addOnPageChangeListener(listener);
    }


    View.OnClickListener onClickListener = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id){
                case R.id.login_tv_log:{
                    setPagerPos(0);
                    break;
                }
                case R.id.login_tv_reg:{
                    setPagerPos(1);
                    break;
                }

            }
        }
    };


    ViewPager.OnPageChangeListener listener = new ViewPager.SimpleOnPageChangeListener(){
        @Override
        public void onPageSelected(int position) {
            setPagerPos(position);
        }
    };


    private void setPagerPos(int position) {
        if (position == 0) {
            logintv.setTextColor(Color.rgb(241,104,173));
            regtv.setTextColor(Color.WHITE);
        }else if (position==1) {
            regtv.setTextColor(Color.rgb(241,104,173));
            logintv.setTextColor(Color.WHITE);
        }
        loginvp.setCurrentItem(position);
    }



    private void initView() {
        logintv =findViewById(R.id.login_tv_log);
        regtv = findViewById(R.id.login_tv_reg);
        loginvp = findViewById(R.id.login_vp);
    }


}